#include "php_evilworlds.hpp"
#include "common_functions.cpp"
#include "evilworlds_api.cpp"
#include "Preprocessor.cpp"
#include "Url.cpp"
#include "Mysql.cpp"
#include "Registry.cpp"
#include "Profiler.cpp"

ZEND_BEGIN_ARG_INFO_EX(arginfo_array_var, 0, 0, 2)
	ZEND_ARG_INFO(1, array)
	ZEND_ARG_INFO(0, key)
	ZEND_ARG_INFO(0, def_value)
ZEND_END_ARG_INFO()

static zend_function_entry functions[] = {
	PHP_FE(array_var, arginfo_array_var)
	PHP_FE_END
}; 

static const zend_module_dep evilworlds_deps[] = {
	{NULL, NULL, NULL}
}; 

zend_module_entry evilworlds_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	STANDARD_MODULE_HEADER_EX, NULL, 
	(zend_module_dep*)evilworlds_deps, 
#endif
	PHP_EVILWORLDS_EXTNAME, 
	functions, 
	PHP_MINIT(evilworlds), 
	PHP_MSHUTDOWN(evilworlds), 
	PHP_RINIT(evilworlds), 
	PHP_RSHUTDOWN(evilworlds), 
	PHP_MINFO(evilworlds), 
#if ZEND_MODULE_API_NO >= 20010901
	PHP_EVILWORLDS_VERSION, 
#endif
	STANDARD_MODULE_PROPERTIES
}; 

extern "C" {
	#ifdef COMPILE_DL_EVILWORLDS
		ZEND_GET_MODULE(evilworlds)
	#endif
}

PHP_MINIT_FUNCTION(evilworlds) {
	init_class_registry(); 
	init_url_preprocessor(); 
	init_text_preprocessor(); 
	init_class_debug_profiler(); 
	init_evilworlds_mysql_driver();  
	return SUCCESS; 
}

PHP_MSHUTDOWN_FUNCTION(evilworlds) {
	destroy_class_registry(); 
	destroy_url_preprocessor(); 
	destroy_text_preprocessor(); 
	destroy_class_debug_profiler(); 
	destroy_evilworlds_mysql_driver();  
	return SUCCESS; 
}

PHP_RINIT_FUNCTION(evilworlds) {
	
	return SUCCESS; 
}

PHP_RSHUTDOWN_FUNCTION(evilworlds) {
	
	return SUCCESS; 
}

PHP_MINFO_FUNCTION(evilworlds) {
	
}

PHP_FUNCTION(array_var) {
	zval *def_val = NULL, **val = NULL; 
	char *key; int key_len; 
	HashTable *array; 
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Hs|z", &array, &key, &key_len, &def_val) != SUCCESS)
		WRONG_PARAM_COUNT; 
	if (zend_hash_find(array, key, key_len + 1, (void **)&val) == SUCCESS)
		RETURN_ZVAL(*val, true, false); 
	if (!def_val)
		RETURN_NULL(); 
	RETURN_ZVAL(def_val, true, false); 
}
